﻿using System;
using System.IO.Compression;
using System.Text;
using System.Web;

namespace Noria.Web.MyMSDN
{
    internal static class Doctor
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public static void PrintSystemInfo()
        {
            var info = new StringBuilder();

            // header
            info.AppendLine();
            info.AppendLine("============================================");
            info.AppendLine("===Logging System Diagnostics Information===");
            info.AppendLine("===--------------------------------------===");

            // hardware

            // OS
            info.AppendFormat(" MachineName: {0}{1}", Environment.MachineName, Environment.NewLine);
            info.AppendFormat(" UserName: {0}{1}", Environment.UserName, Environment.NewLine);
            info.AppendFormat(" OSVersion: {0}{1}", Environment.OSVersion, Environment.NewLine);
            info.AppendFormat(" SystemDirectory: {0}{1}", Environment.SystemDirectory, Environment.NewLine);
            info.AppendFormat(" SystemPageSize: {0}{1}", Environment.SystemPageSize, Environment.NewLine);
            info.AppendFormat(" ProcessorCount: {0}{1}", Environment.ProcessorCount, Environment.NewLine);
            info.AppendFormat(" LogicalDrives: {0}{1}", string.Join(" ", Environment.GetLogicalDrives()), Environment.NewLine);
            info.AppendFormat(" Is64BitOperatingSystem: {0}{1}", Environment.Is64BitOperatingSystem, Environment.NewLine);
            info.AppendFormat(" Is64BitProcess: {0}{1}", Environment.Is64BitProcess, Environment.NewLine);
            info.AppendLine();

            // app domain
            info.AppendFormat(" AppDomain.Id: {0}{1}", AppDomain.CurrentDomain.Id, Environment.NewLine);
            info.AppendFormat(" FriendlyName: {0}{1}", AppDomain.CurrentDomain.FriendlyName, Environment.NewLine);
            info.AppendFormat(" IsFullyTrusted: {0}{1}", AppDomain.CurrentDomain.IsFullyTrusted, Environment.NewLine);
            info.AppendFormat(" IsHomogenous: {0}{1}", AppDomain.CurrentDomain.IsHomogenous, Environment.NewLine);
            info.AppendLine();

            // IIS

            // ASP.NET

            // footer
            info.AppendLine("============================================");

            log.Info(info.ToString());
        }

        public static string AnalyzeEncoding(HttpContext context)
        {
            var ret = "none";
            var encoding = context.Request.Headers["Accept-encoding"];

            if (encoding != null)
            {
                encoding = encoding.ToLowerInvariant();
                if (encoding.Contains("gzip"))
                {
                    ret = "gzip";
                    context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
                    context.Response.AppendHeader("Content-Encoding", ret);
                }
                else if (encoding.Contains("deflate"))
                {
                    ret = "deflate";
                    context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
                    context.Response.AppendHeader("Content-Encoding", ret);
                }
            }

            return ret;
        }
    }
}